package fileExplorer;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/**
 * This class creates and runs the browse dialog.
 */
public class BrowseFileGUI{
/* {author=George Siegle}*/
	
	private JFrame browseFrame = new JFrame();
	private JTextPane currentFolderPathPane = new JTextPane();
	private FileAndFolderSearch searchFileAndFolderContents = new FileAndFolderSearch();
	private DefaultListModel<String> folderContentsModel = new DefaultListModel<String>();
	private JList<String> displayFoldersList = new JList<String>(folderContentsModel);
	private boolean isFolder = true;
	private String rootDir = File.listRoots()[0].toString();
	private String splitPath[];
	private JLabel currentFolderLabel = new JLabel("Current Folder Directory: ");
	private JLabel selectFolderLabel = new JLabel("Select Folder or File:");
	private JButton openFolderOrFileButton = new JButton("Open");
	private JButton backButton = new JButton("Back");
	private GridBagConstraints browseWindowConstraints = new GridBagConstraints();
	private JScrollPane displayFoldersScroller = new JScrollPane(displayFoldersList);;
	private DefaultListModel<String> fileListModelTmp;
	private String[] folderContents;
	private JTextPane filePathPaneTmp;
	
	/**
	 * Constructor that starts the creation of the browse dialog.
	 */
	public BrowseFileGUI(DefaultListModel<String> fileListModel, JTextPane filePathPane) {
	/* {author=George Siegle}*/
		fileListModelTmp = fileListModel;
		filePathPaneTmp = filePathPane;
		browseComputer();
		fileListModel = fileListModelTmp;
		filePathPane = filePathPaneTmp;
	}
	
	/** 
	 *  This method uses the FileAnFolderSearch class to search for files and folders at the directory location that the user is browsing. 
	 */
	private void browseComputer() {
	/* {author=George Siegle}*/
		fileListModelTmp.clear();
		folderContentsModel.clear();
		searchFileAndFolderContents.setFolderOrFileSelected(rootDir);
		searchFileAndFolderContents.searchForFilesAndFolders();
		folderContents = new String[searchFileAndFolderContents.getContents().length];
		folderContents = searchFileAndFolderContents.getContents();
		  
		for (int index = 0; index < folderContents.length; index++) {
			splitPath = folderContents[index].split("[\\\\]");
			folderContentsModel.addElement(splitPath[splitPath.length - 1]);
		}

		browseFrame.setTitle("Browse For File");
		browseFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		browseFrame.getContentPane().setLayout(new GridBagLayout());
		  
		browseWindowConstraints.fill = GridBagConstraints.HORIZONTAL;
		browseWindowConstraints.weightx = 0.5;
		browseFrame.getContentPane().setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
		browseWindowConstraints.gridx = 2;
		browseWindowConstraints.gridy = 0;
		displayFoldersList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		displayFoldersList.setLayoutOrientation(JList.VERTICAL);
		displayFoldersScroller.setPreferredSize(new Dimension(250, 80));
		browseFrame.getContentPane().add(displayFoldersScroller, browseWindowConstraints);
		  
		browseWindowConstraints.fill = GridBagConstraints.HORIZONTAL;
		browseWindowConstraints.weightx = 0.5;
		browseWindowConstraints.gridx = 3;
		browseWindowConstraints.gridy = 0;
		browseFrame.getContentPane().add(selectFolderLabel, browseWindowConstraints);
		  
		browseWindowConstraints.fill = GridBagConstraints.HORIZONTAL;
		browseWindowConstraints.weightx = 0.5;
		browseWindowConstraints.gridx = 3;
		browseWindowConstraints.gridy = 1;
		browseFrame.getContentPane().add(currentFolderLabel, browseWindowConstraints);
		  
		currentFolderPathPane.setText(rootDir);
		browseWindowConstraints.fill = GridBagConstraints.HORIZONTAL;
		browseWindowConstraints.weightx = 0.5;
		browseWindowConstraints.gridx = 2;
		browseWindowConstraints.gridy = 1;
		browseFrame.getContentPane().add(currentFolderPathPane, browseWindowConstraints);
		  
		browseWindowConstraints.fill = GridBagConstraints.HORIZONTAL;
		browseWindowConstraints.weightx = 0.5;
		browseWindowConstraints.gridx = 1;
		browseWindowConstraints.gridy = 1;
		browseFrame.getContentPane().add(openFolderOrFileButton, browseWindowConstraints);
		  
		browseWindowConstraints.fill = GridBagConstraints.HORIZONTAL;
		browseWindowConstraints.weightx = 0.5;
		browseWindowConstraints.gridx = 0;
		browseWindowConstraints.gridy = 1;
		browseFrame.getContentPane().add(backButton, browseWindowConstraints);
		  
		openFolderOrFileButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				searchFileAndFolderContents.setFolderOrFileSelected(currentFolderPathPane.getText());
				if (!searchFileAndFolderContents.isFile()) {
					searchFileAndFolderContents.searchForFilesAndFolders();
					folderContents = new String[searchFileAndFolderContents.getContents().length];
					folderContents = searchFileAndFolderContents.getContents();
					  
					folderContentsModel.clear();
					for (int index = 0; index < folderContents.length; index++) {
						splitPath = folderContents[index].split("[\\\\]");
						folderContentsModel.addElement(splitPath[splitPath.length - 1]);
					}
				}
				else {
					filePathPaneTmp.setText(searchFileAndFolderContents.getFolderOrFileSelected());
					((JFrame)filePathPaneTmp.getTopLevelAncestor()).pack();
					fileListModelTmp.addElement(searchFileAndFolderContents.getFolderOrFileSelected());
					isFolder = false;
					browseFrame.setVisible(false);
					browseFrame.dispose();
				}
				currentFolderPathPane.setText(searchFileAndFolderContents.getFolderOrFileSelected());
			}
		});
		  
		backButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent event) {
				if (!searchFileAndFolderContents.getFolderOrFileSelected().equals(rootDir)) {
					String previousFolder = searchFileAndFolderContents.getPreviousSelectedFolder();
					searchFileAndFolderContents.setFolderOrFileSelected(previousFolder);
					searchFileAndFolderContents.searchForFilesAndFolders();
					folderContents = new String[searchFileAndFolderContents.getContents().length];
					folderContents = searchFileAndFolderContents.getContents();
					  
					folderContentsModel.clear();
					for (int index = 0; index < folderContents.length; index++) {
						splitPath = folderContents[index].split("[\\\\]");
						folderContentsModel.addElement(splitPath[splitPath.length - 1]);
					}
					currentFolderPathPane.setText(searchFileAndFolderContents.getFolderOrFileSelected());
				}
			}
		});
		if(isFolder) {
			browseFrame.pack();
			browseFrame.setVisible(true);
			displayFoldersList.addListSelectionListener(new ListSelectionListener() {
				@Override
				public void valueChanged(ListSelectionEvent arg0) {
					if (!arg0.getValueIsAdjusting()) {
						if (displayFoldersList.getSelectedIndex() != -1) {
							currentFolderPathPane.setText(folderContents[displayFoldersList.getSelectedIndex()]);
							((JFrame)currentFolderPathPane.getTopLevelAncestor()).pack();
						}
					}
				}
			});
		}
	}
}
